﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="PayQuery.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：PayQuery.cs
// 创建时间：2015-01-20 9:48
// 修改时间：2015-01-20 9:48
// </summary>
// ---------------------------------------------------------------------------------------
#endregion
namespace Payment.ChinaPay
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// 银联查询
    /// </summary>
    public class PayQuery : PaySubBase
    {
        /// <summary>
        /// The config.
        /// </summary>
        private ChinaPayConfig config;

        /// <summary>
        /// 提交方式
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        protected override string Method()
        {
            return "POST";
        }

        /// <summary>
        /// 支付订单号
        /// </summary>
        public string OrdId { get; set; }

        /// <summary>
        /// 该笔订单交易日期
        /// </summary>
        public string TransDate { get; set; }

        /// <summary>
        /// 银联查询
        /// </summary>
        /// <param name="config">
        /// ChinaPayConfig账户配置
        /// </param>
        public PayQuery(ChinaPayConfig config, string ordid, string transDate)
        {
            this.OrdId = ordid;
            this.TransDate = transDate;
            this.config = config;
            this.Gateway = "http://console.chinapay.com/QueryWeb/processQuery.jsp";


            this.Pay = new NetPay();
            var buildKey = this.Pay.buildKey(config.MerId, config.KeyUsage, config.KeyFile);
            if (!buildKey)
            {
                throw new Exception("密钥路径错误");
            }
        }

        public NetPay Pay { get; set; }

        protected override Dictionary<string, string> GetSubData()
        {
            var d = new Dictionary<string, string>();
            d.Add("MerId", config.MerId);
            d.Add("TransType", "0001");
            d.Add("OrdId", OrdId);
            d.Add("TransDate", TransDate);
            d.Add("Version", "20060831");
            d.Add("Resv", "");
            d.Add("ChkValue", this.Sign());
            return d;
        }

        /// <summary>
        /// 签名
        /// </summary>
        /// <returns></returns>
        private string Sign()
        {
            var plainData = this.config.MerId + this.TransDate + this.OrdId + "0001";
            if (this.Pay.PrivateKeyFlag)
            {
                return this.Pay.Sign(plainData);

            }
            else
            {
                throw new Exception("进行签名失败");
            }

        }
    }
}